import time
from pathlib import Path

from PIL import Image


def get_all_pixels(image_path):
    """
    获取图片的所有像素数据

    返回:
        二维列表，包含整个图片的像素RGB值
    """
    img = Image.open(image_path)
    width, height = img.size
    pixels = list(img.getdata())
    return [pixels[i * width:(i + 1) * width] for i in range(height)]


if __name__ == '__main__':
    print('开始')
    timestamp = int(time.time())
    input_image = "D:\\2\\map\\111111111.jpg"
    kuan = 0
    gao = 0
    output_image = input_image.replace("\\2\\", "\\3\\")
    original_image = Image.open(input_image)
    # pixel = original_image.getpixel((5600, 4070))
    # print(pixel)
    x = 0
    y = 0
    for x_1 in reversed(range(original_image.width)):
        pixel = original_image.getpixel((x_1, 10))
        if pixel == (1, 1, 1):
        # if pixel == (0, 0, 0):
        # if pixel == (255, 255, 255):
            print("黑色的x  " + str(x_1))
            x = x_1
    for x_1 in reversed(range(original_image.height)):
        pixel = original_image.getpixel((10, x_1))
        if pixel == (1, 1, 1):
        # if pixel == (0, 0, 0):
        # if pixel == (255, 255, 255):
            print("黑色的y  " + str(x_1))
            y = x_1
    bottomx = original_image.width
    if x != 0:
        bottomx = x
    bottomy = original_image.height
    if y != 0:
        bottomy = y
    print("*******************")
    print(str(original_image.width) + "   " + str(bottomx + kuan))
    print(str(original_image.height) + "   " + str(bottomy + gao))

    crop_area = (0, 0, bottomx + kuan, bottomy + gao)
    # crop_area = (64, 55, 6384, 5305)
    cropped_image = original_image.crop(crop_area)
    dst = Path(output_image)
    dst.parent.mkdir(parents=True, exist_ok=True)
    cropped_image.save(output_image)
    print(f"图片已成功裁剪并保存到 {output_image}")
    endTime = int(time.time())
    print(str(endTime - timestamp))
    print('结束')

# import time
# from pathlib import Path
#
# from PIL import Image
#
# def get_all_pixels(image_path):
#     """
#     获取图片的所有像素数据
#
#     返回:
#         二维列表，包含整个图片的像素RGB值
#     """
#     img = Image.open(image_path)
#     width, height = img.size
#     pixels = list(img.getdata())
#     return [pixels[i * width:(i + 1) * width] for i in range(height)]
#
#
# if __name__ == '__main__':
#     print('开始')
#     timestamp = int(time.time())
#     for aaaa in range(56):
#         input_image = "D:\\2\\newscene\\"+str(aaaa+4000)+".jpg"
#         kuan = 0
#         gao = 0
#         output_image = input_image.replace("\\2\\", "\\3\\")
#         original_image = Image.open(input_image)
#         x = 0
#         y = 0
#         for x_1 in reversed(range(original_image.width)):
#             pixel = original_image.getpixel((x_1, 10))
#             if pixel == (1, 1, 1):
#                 print("黑色的x  " + str(x_1))
#                 x = x_1
#         for x_1 in reversed(range(original_image.height)):
#             pixel = original_image.getpixel((10, x_1))
#             if pixel == (1, 1, 1):
#                 print("黑色的y  " + str(x_1))
#                 y = x_1
#         bottomx = original_image.width
#         if x != 0:
#             bottomx = x
#         bottomy = original_image.height
#         if y != 0:
#             bottomy = y
#         print("*******************")
#         print(str(original_image.width) + "   " + str(bottomx + kuan))
#         print(str(original_image.height) + "   " + str(bottomy + gao))
#
#         # crop_area = (0, 0, bottomx + kuan, bottomy + gao)
#         crop_area = (0, 0, 820, 640)
#         cropped_image = original_image.crop(crop_area)
#         dst = Path(output_image)
#         dst.parent.mkdir(parents=True, exist_ok=True)
#         cropped_image.save(output_image)
#         print(f"图片已成功裁剪并保存到 {output_image}")
#
#     endTime = int(time.time())
#     print(str(endTime - timestamp))
#     print('结束')
